const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type,userToKen");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200); //让options尝试请求快速结束
    else
        next();
})
app.use(bodyParser.urlencoded({
    extended: false
})); //extended为false，使用使用系统模块querystring处理
app.use(bodyParser.json());
// 连接数据库
mongoose.connect('mongodb://localhost:27017/qulala', { useNewUrlParser: true, useUnifiedTopology: true });
// 监听连接状态
mongoose.connection.once('open', () => {
    console.log('数据库连接成功!');
    app.listen(3021, () => {
        console.log('http://localhost:3021');
    })
})

var user = require('./router/user');
var car = require('./router/car/car');
app.use('/user', user)
app.use('/car', car)

